;(function ($) {
	//入口
	$.fn.handMgn=function(options){
		//创建对象 
		$.handMgn = $.extend($.handMgn||{},new myHandMgn(this, options));  
		//初始化
		$.handMgn.getData({"operation":"刷新"});  
	}
	
	//构造函数
    var myHandMgn = function(ele, opt) {   	
    	//选择器对象    	
        this.$element=ele;   
        //默认参数
        this.defaults = {
    		//当前选择得楼层
    		onSelFloor:{"floor":""},
    		//当前选择得手牌
    		onSelTh:{"th":""},
    		//当前选择得类型
    		onSelLx:{"cwlx":""},
    		returnBack:function(){},
    		dataBack:function(){},
    		resetBack:function(){}
        },      
        //初始化参数
        this.options = $.extend({}, this.defaults, opt);
    }

    //属性方法
    myHandMgn.prototype = {
    		//获取数据
    		getData:function(params){
    			var $t=$this=this; 
        		var $opt=$this.options;
        		var el=$this.$element;

        		var param={};
        		var arg={"arg":"手牌管理"};
        		arg=$.extend({},arg,$opt.onSelFloor,$opt.onSelTh,$opt.onSelLx,params);
        		param.arg=arg;
                $t.wait();
                $.HSAPI_CALLPROCEDURE2(param,"SPAct_thSynSearch",{},function(data){
                    $t.goon();
                    if(data.RETURN==1){ 
                        $t.makeOwnProperty(data); 
                        if(params.operation==="刷新"){ 
                            el.empty();
                            $t.handMgnhtml(data.floor_list);
                            $t.makeHandList(data.ct_th_floor); 
                            $t.reSetScrollTop();
                            $opt.resetBack();
                        }
                        if(params.operation==="查询"){ 
                            if(data.th!=""){
                                $t.setOnSelFloor(data.floor[0].floor);  
                                $t.setOnSelTh(data.th);
                                $t.handMgnhtml(data.floor);
                            }
                            $t.makeHandList(data.ct_th_floor);
                            $t.reSetScrollTop();  
                            $opt.resetBack();
                        }
                    }else{
                        if(data.hasOwnProperty("gritter")){
                            if(data.hasOwnProperty("gritterstyle"))
                                jf_successDelayed(data.gritter,2000,data.gritterstyle);
                            else
                                jf_successDelayed(data.gritter,2000); 
                        }else{
                            $.HSAPI_ALERT(data.MSG)
                        }
                    }
                },function(data){
                    $t.goon();
            		$.HSAPI_ALERT(JSON.stringify(data));
                })
    		},
    		//添加房间
    		handMgnhtml:function(data){ 
        		var $t=$this=this; 
        		var $opt=$this.options;
        		var el=$this.$element; 

        		data.map(x => { 
        			var uuid=$t.getUUID();
        			var item='<div data-floor="'+x.floor+'" class="panel panel-default">'+ 
        				   		'<div class="panel-heading">'+
        							'<div class="profile-activity clearfix">'+  
        							   '<div>'+ 
        							    '<a class="accordion-toggle collapsed stylea" data-toggle="collapse" data-parent="#accordion" data-id="'+x.floor+'" href="#'+uuid+'" aria-expanded="false">'+
        							    	'<i class="bigger-110 ace-icon fa fa-angle-down" data-icon-hide="ace-icon fa fa-angle-down" data-icon-show="ace-icon fa fa-angle-right"></i>'+
        							    	'<span class="collapsedTitle">区域:</span><span class="collapsedText">'+x.floor+'</span>&nbsp'+ 
        							    	'<span class="collapsedTitle">类型:</span><span class="collapsedText">'+x.cwlx_c+'</span>&nbsp'+
        							    	'<span class="collapsedTitle">共:</span><span class="collapsedText">'+x.total_counts+'</span>&nbsp'+
        							    	'<span class="collapsedTitle">占用:</span><span class="collapsedText">'+x.v_counts+'</span>&nbsp'+
        							    	'<span class="collapsedTitle">可用:</span><span class="collapsedText">'+x.o_counts+'</span>&nbsp'+
        							    	'<span class="collapsedTitle">维修:</span><span class="collapsedText">'+x.oo_counts+'</span>&nbsp'+ 
        							    	'<span class="collapsedTitle">消费金额:</span><span class="collapsedText">'+(x.hasOwnProperty("xfje")?x.xfje:"0")+'</span>&nbsp'+    
        						    	'</a>'+ 
        							   '</div>'+ 
        							   '<div class="tools action-buttons">'+   
//        								    '<a id="'+$t.getUUID()+'" data-edit-id="'+uuid+'" data-floor="'+x.floor+'" href="javaScript:void(0);" class="blue">'+
//        								    	'<i class="ace-icon fa fa-pencil bigger-170"></i>'+ 
//        								    '</a>'+ 
        								    '<a style="margin-left:40px;" id="'+$t.getUUID()+'" data-ledger-id="'+uuid+'" data-floor="'+x.floor+'" href="javaScript:void(0);" class="blue">'+
        								    	'<i class="ace-icon fa fa-jpy bigger-170"></i>'+ 
        								    '</a>'+
        							   '</div>'+ 
        							  '</div>'+
        				    	'</div>'+ 
        				    	'<div class="panel-collapse collapse clearfix" id="'+uuid+'" aria-expanded="false">'+     
        						     '<div data-id="'+uuid+'" class="panel-body" style="padding: 0px;padding-left:20px;">'+'</div>'+    
        				    	'</div>'+ 
        				   '</div>';
        				   if(el.find("div[data-floor='"+x.floor+"']").length==1){   
        					   var brother=el.find("div[data-floor='"+x.floor+"']").prev();   
        					   el.find("div[data-floor='"+x.floor+"']").remove();
        					   if(brother.length==1){
        						   $(item).insertAfter(brother);
        					   } else
        						   el.prepend(item);
        					   $("#"+uuid).collapse('show');
        				   }else{
        					   el.append(item);
        					   if(x.floor===$opt.onSelFloor.floor)    
        						   $("#"+uuid).collapse('show');
        				   } 
        				   
        				   el.find("a[href='#"+uuid+"']").bind("click",function(){ 
        					   $t.setOnSelFloor($(this).attr("data-id"));
        					   $t.setOnSelTh(""); 
        					   if($(this).hasClass("collapsed")){
        						   $t.getData({"operation":"查询"});   
        						   return;
        					   }
        					   el.find("div[data-id='"+uuid+"']").empty();  
        				   }); 
        				   
        				   el.find("a[data-ledger-id='"+uuid+"']").bind("click",function(){ 
         					   var params="?&th="+$opt.onSelTh.th; 
         					//    replaceUrl("home#page/spa_handMgn"+params);
							// 	window.open("home#page/spa_guestAccount"+params,"_self"); 
								window.location.href = "#/html/cashier_mana/spa_guestAccount.html"+params
         				   });
        		});
        		
    		},
    		//构造手牌列表
    		makeHandList:function(data){ 
    			var $t=$this=this; 
        		var $opt=$this.options;
        		var el=$this.$element; 
        		var floor=$opt.onSelFloor.floor;
        		var itemDom=el.find("div[data-floor='"+floor+"']").find(".panel-body"); 
        		itemDom.empty();
        		
        		data.map(x => {
        			
        			var	xfje=x.hasOwnProperty("xfje")?x.xfje:"";
        			var dfje=(x.hasOwnProperty("dfje")&&x.dfje>0.00)?"("+x.dfje+")":"";
        			
        			var handpanel_info={};
        			if(x.hasOwnProperty("handpanel_info")&&x.handpanel_info!="")
        				handpanel_info=JSON.parse(x.handpanel_info);
        			
        			var ico_info=[]
        			var ico_info_text="";
        			if(x.hasOwnProperty("ico_info")&&x.ico_info!="[]")
        				ico_info=JSON.parse(x.ico_info);
        			$.each(ico_info,function(key,value){ 
        				ico_info_text += getTextStyle(value); 
    				})

					var height="60px";
					if(handpanel_info.hasOwnProperty("height"))
						height=handpanel_info.height;
					var width="100px";
					if(handpanel_info.hasOwnProperty("width")) 
						width=handpanel_info.width;
					var border="";
					var defaultBorderColor="#AAAAAA"
					if(handpanel_info.hasOwnProperty("border")&&handpanel_info.border>"0"&&handpanel_info.border!="")  
						border="border: "+handpanel_info.border+"px solid "+defaultBorderColor+";";  
					var focsuColor="";
					if(handpanel_info.hasOwnProperty("focuscolor"))
						focsuColor=handpanel_info.focuscolor;
						
					var padding="padding: 0px;";
					if(handpanel_info.hasOwnProperty("padding")&&handpanel_info.padding!="")      
						padding="padding: "+handpanel_info.padding+"px;";
						
        			var item='<div data-focus-color="'+focsuColor+'" class="box" data-body-floor="'+x.floor+'" data-body-bh="'+x.bh+'" style="'+border+' background-color:'+handpanel_info.bgcolor+';width:'+width+' !important; height:'+height+' !important; '+padding+' float:left;margin-left:5px;margin-top:5px;">'+
    						     '<div style="width: 100%;height:33%;float: left;"><span style="color:'+handpanel_info.tcolor+'">'+x.bh+'</span>'+ 
    						      	'<small class="text-muted" style="color:'+handpanel_info.tcolor+' !important; font-size:60%;  float:right;">&nbsp;'+ico_info_text+ '</small>'+
    						     '</div>'+
    						     '<div style="width: 100%;height:33%;float: left;">'+ 
	 					      	 	'<span style="float:right;color:'+handpanel_info.tcolor+'">'+xfje+'</span>'+  
	 						     '</div>'+
    						     '<div style="width: 100%;height:33%;float: left;white-space:nowrap; overflow:hidden; text-overflow:ellipsis;">'+  
    						     	'<span style="float:right;color:'+handpanel_info.tcolor+'">'+dfje+'</span>'+  
    				      	 	'</div>'+
    					     '</div>';

        			itemDom.append(item); 
        			
        			itemDom.children("div:last-child").bind("click",function(){ 
        				var focsuColor=$(this).attr("data-focus-color");
    					if($(this).hasClass("selected"))
    						return;
    					$(".selected").css("border-color",defaultBorderColor); 
    					$(".selected").removeClass("selected");
    					$(this).addClass("selected");
    					$(this).css("border-color",focsuColor);
    					$t.setOnSelTh($(this).attr("data-body-bh"));
    				});  
    				if(itemDom.children("div:last-child").attr("data-body-bh")==$opt.onSelTh.th){
    					$(".selected").css("border-color",defaultBorderColor);
    					$(".selected").removeClass("selected");
    					itemDom.children("div:last-child").addClass("selected"); 
    					itemDom.children("div:last-child").css("border-color",focsuColor);
    				}
        		});
    		},
    		makeHandListDetails:function(data){ 
    			var $t=$this=this; 
        		var $opt=$this.options;
        		var el=$this.$element;
        		
        		if(!$.isFunction($().guest_xsxm)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/spa_guestList/jquery.plugin.guest_xsxm.js");
		            $.ajaxSetup({ async: true });
				}
        		
        		var floor=$opt.onSelFloor.floor;
        		var itemDom=el.find("div[data-floor='"+floor+"']").find(".panel-body"); 
        		itemDom.empty();
        		
        		var brzd_json={};
        		$.each(data.ct_th_floor,function(key,value){    
        			brzd_json[value.th] = value
        		})

        		$(itemDom).guest_xsxm({ 
        			xsxm_jdh_array:data.ct_brxsxm_floor, 
        			brzd_json:brzd_json,
        			dblClickBack : function(data){
        				
        			},
        			loadcomplete : function(){
        				reSetScrollTop();
        			}
        		})
    		},
    		//属性
    		setOnSelFloor:function(value){
    			this.options.onSelFloor={"floor":value};
    		},
    		setOnSelTh:function(value){
    			this.options.onSelTh={"th":value};
    		},
    		setOnSelLx:function(value){
    			this.options.onSelLx={"cwlx":value};
    		},
    		//属性处理
    		makeOwnProperty:function(data){
    			var $t=$this=this; 
        		var $opt=$this.options;
        		
    			if(data.hasOwnProperty("floor_list")&&data.floor_list.length>0&&$opt.onSelFloor.floor=="") 
					$t.setOnSelFloor(data.floor_list[0].floor);
				if(data.hasOwnProperty("ct_th_floor")&&data.ct_th_floor.length>0&&$opt.onSelTh.th=="")  
					$t.setOnSelTh(data.ct_th_floor[0].bh);
    		},
    		//交互等待
    		wait:function(){
    			if($("#pageOverlay").length>0)
    				$("#pageOverlay").removeClass("hide");
    			if($("#loading").length>0)
    				$("#loading").removeClass("hide");
    		},
    		//交互结束
    		goon:function(){
    			if($("#pageOverlay").length>0)
    				$("#pageOverlay").addClass("hide");
    			if($("#loading").length>0)
    				$("#loading").addClass("hide");
    		},
    		//唯一标识
    		getUUID:function(){
    			var d = new Date().getTime();
			    if (window.performance && typeof window.performance.now === "function") {
			        d += performance.now();
			    }
			    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
			        var r = (d + Math.random() * 16) % 16 | 0;
			        d = Math.floor(d / 16);
			        return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
			    });
			    return uuid;
    		},
    		//滚动条位置
    		reSetScrollTop:function(){ 
    			var $t=$this=this; 
        		var $opt=$this.options;
        		var el=$this.$element;  
        		
    			if($opt.onSelFloor.floor!=""){
    				el.scrollTop(0); 
    				var topHeithg=el.find("div[data-floor='"+$opt.onSelFloor.floor+"']").offset().top;
    				el.scrollTop(topHeithg-200);   
    			}
    		},
    		//获取项目路径
    		getContextPath:function(){
    			var pathName = document.location.pathname;
    			var index = pathName.substr(1).indexOf("/");
    			var path = pathName.substr(0, index + 1);
    			return path;
    		},
    		//替换当前URL
    		replaceUrl:function(url){
    			var json={time:new Date().getTime()};
        		window.history.replaceState(json,"",url);
    		}
	}	
})(jQuery);